package chess_game_5;

import java.util.List;

// 打印棋盘与棋子(棋子排列信息现在定义在了各个游戏模式中了)
public class DisplayBoard {

    public void printFullUI(Game currentGame, List<Game> games) {
        // lab5优化，将游戏信息打印在棋盘右侧
        clearScreen();

        // 获取棋盘、游戏信息、游戏列表的字符串内容
        String boardStr = getGameBoard(currentGame);
        String infoStr = getGameInfo(currentGame);
        String listStr = getGameList(games);

        // 将棋盘和右侧信息拼接为左右分栏
        String[] boardLines = boardStr.split("\n");
        String[] infoLines = (infoStr + "\n" + listStr).split("\n");

        // 打印棋盘和右侧信息
        int maxLines = Math.max(boardLines.length, infoLines.length);
        for (int i = 0; i < maxLines; i++) {
            String boardLine = (i < boardLines.length) ? boardLines[i] : "";
            String infoLine = (i < infoLines.length) ? infoLines[i] : "";
            System.out.printf("%-20s %s\n", boardLine, infoLine); // 左侧棋盘占20字符宽
        }
        /*
         * clearScreen();
         * // 打印左侧棋盘
         * printChessBoard(currentGame);
         * // 打印中间信息
         * printGameInfo(currentGame);
         * // 打印右侧游戏列表
         * printGameList(games);
         */
    }

    // lab5版本
    private String getGameBoard(Game game) {
        StringBuilder strBuild = new StringBuilder();
        char[][] displayBoard = createDisplayBoard(game);

        // append 方法是 StringBuilder 类的一部分，用于将字符串或其他数据类型追加到 StringBuilder 对象的末尾。
        // 这里是将棋盘的列标（A-H）添加到字符串构建器中
        strBuild.append("   A B C D E F G H\n");

        for (int i = 0; i < Chess.BOARD_SIZE; i++) {
            strBuild.append((i + 1) + "  ");
            for (int j = 0; j < Chess.BOARD_SIZE; j++) {
                strBuild.append(displayBoard[i][j]).append(" ");
            }
            strBuild.append("\n");
        }
        return strBuild.toString();
    }

    // 与lab4版本的printChessBoard方法相同，更名为createDisplayBoard
    private char[][] createDisplayBoard(Game game) {
        char[][] displayBoard = new char[Chess.BOARD_SIZE][Chess.BOARD_SIZE];
        char[][] originalBoard = game.getBoard();
        List<int[]> legalMoves = game.getLegalMoves();

        // 复制原始棋盘
        for (int i = 0; i < Chess.BOARD_SIZE; i++) {
            System.arraycopy(originalBoard[i], 0, displayBoard[i], 0, Chess.BOARD_SIZE);
        }

        // 如果是黑白棋模式，标记合法落子位置
        if (game.getMode().equals("reversi")) {
            for (int[] pos : legalMoves) {
                displayBoard[pos[0]][pos[1]] = '+';
            }
        }

        return displayBoard;
    }

    private String getGameInfo(Game game) {
        return "当前游戏: #" + game.getGameId() + "\n" +
                "模式: " + game.getMode() + "\n" +
                "玩家: " + game.getPlayer().getCurrentPlayerName() + "\n" +
                (game.getMode().equals("reversi") ? "得分: " + game.getScoreInfo()
                        : game.getMode().equals("gomoku") ? "轮数: " + game.getScoreInfo() : "");
    }

    private String getGameList(List<Game> games) {
        StringBuilder strBuild = new StringBuilder("\n游戏列表:\n");
        for (Game g : games) {
            strBuild.append(g.getGameId()).append(". ").append(g.getMode()).append("\n");
        }
        return strBuild.toString();
    }

    // 打印棋盘，lab4版本
    /*
     * private void printChessBoard(Game game) {
     * char[][] displayBoard = new char[Chess.BOARD_SIZE][Chess.BOARD_SIZE];
     * char[][] originalBoard = game.getBoard();
     * List<int[]> legalMoves = game.getLegalMoves();
     * 
     * // 复制原始棋盘并标记合法位置
     * for (int i = 0; i < Chess.BOARD_SIZE; i++) {
     * System.arraycopy(originalBoard[i], 0, displayBoard[i], 0, Chess.BOARD_SIZE);
     * }
     * if (game.getMode().equals("reversi")) {
     * for (int[] pos : legalMoves) {
     * displayBoard[pos[0]][pos[1]] = '+';
     * }
     * }
     * 
     * // 打印棋盘
     * System.out.println("   A B C D E F G H");
     * for (int i = 0; i < Chess.BOARD_SIZE; i++) {
     * System.out.print((i + 1) + "  ");
     * for (int j = 0; j < Chess.BOARD_SIZE; j++) {
     * System.out.print(displayBoard[i][j] + " ");
     * }
     * System.out.println();
     * }
     * }
     */

    // 打印游戏信息,lab4版本
    /*
     * private void printGameInfo(Game game) {
     * System.out.println("\n当前游戏: #" + game.getGameId());
     * System.out.println("模式: " + game.getMode());
     * System.out.println("玩家: " + game.getPlayer().getCurrentPlayerName());
     * 
     * if (game.getMode().equals("reversi")) {
     * System.out.println("得分: " + game.getScoreInfo());
     * } else if (game.getMode().equals("gomoku")) {
     * System.out.println("轮数: " + game.getScoreInfo());
     * }
     * }
     */

    // 打印游戏列表,lab4版本
    /*
     * private void printGameList(List<Game> games) {
     * System.out.println("\n游戏列表:");
     * for (Game g : games) {
     * System.out.printf("%d. %s%n", g.getGameId(), g.getMode());
     * }
     * }
     */

    // 清空控制台
    public void clearScreen() {
        try {
            if (System.getProperty("os.name").contains("Windows")) {
                new ProcessBuilder("cmd", "/c", "cls").start().waitFor();
            } else {
                System.out.print("\033[H\033[2J");
            }
        } catch (Exception e) {
            System.out.println("\n".repeat(50));
        }
    }

    // 解析落子位置（如"A1"→[0,0]）
    public int[] parsePosition(String input) {
        if (input.length() != 2)
            return null;

        char rowChar = Character.toLowerCase(input.charAt(0)); // 行
        char colChar = input.charAt(1); // 列

        // 验证输入范围
        if (colChar < 'a' || colChar > 'h')
            return null;
        if (rowChar < '1' || rowChar > '8')
            return null;

        return new int[] { rowChar - '1', colChar - 'a' }; // 转为数组下标
    }
}